﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.effects {
    import flash.events.*;
    import Engine.Events.*;
    import flash.display.*;
    import Engine.Helpers.*;
    import flash.utils.*;
    import Classes.*;
    import com.greensock.*;
    import GameMode.*;
    import com.greensock.easing.*;

    public class MultiPickEffect extends MapResourceEffect {

        public static const MULTIPICK_PICK_CHANGED:String = "MultiPickPickChanged";
        public static const OPTION_ACTION_ON_SELECT:String = "OptionActionOnSelect";
        protected static const EFFECT_NAME:String = "multiPick";
        protected static const PICK_TWEEN_IN_TIME:Number = 0.75;
        protected static const PICK_SLOTS:int = 3;
        protected static const PICK_SLOT_MAP:Array = new Array(0, -1, 1);
        protected static const PICK_FLOAT_DISTANCE:int = 5;
        protected static const PICK_OFFSET_Y:int = 10;
        protected static const FOLD_PICK_OFFSET_X:int = 10;
        protected static const FOLD_PICK_OFFSET_Y:int = 10;
        protected static const MOVE_PICK_OFFSET_X:int = 5;
        protected static const MOVE_PICK_OFFSET_Y:int = 5;
        protected static const PHASE_NONE:int = 1;
        protected static const PHASE_FOLDING:int = 10;
        protected static const PHASE_FOLDED:int = 11;
        protected static const PHASE_UNFOLDING:int = 20;
        protected static const PHASE_UNFOLDED:int = 21;

        protected var m_phase:int;
        protected var m_stage:String;
        protected var m_picks:Array;
        protected var m_stageToPick:Dictionary = null;
        protected var m_highlighted:StagePickEffect;
        protected var m_pickEventCount:int = 0;
        protected var m_animateEventCount:int = 0;
        protected var m_options:Dictionary = null;

        public function MultiPickEffect(_arg1:MapResource){
            super(_arg1);
            this.m_phase = PHASE_NONE;
            this.m_stage = null;
            this.m_highlighted = null;
            this.m_pickEventCount = 0;
            this.m_animateEventCount = 0;
            this.setDefaultOptions();
            this.m_picks = new Array();
            this.m_stageToPick = new Dictionary();
            this.mouseEnabled = true;
            this.mouseChildren = true;
            this.addEventListener(MouseEvent.MOUSE_OVER, this.animatePicksUnfold, false, 0, true);
            this.addEventListener(MouseEvent.MOUSE_OUT, this.animatePicksFold, false, 0, true);
        }
        protected function setDefaultOptions():void{
            this.m_options = new Dictionary();
            this.m_options[OPTION_ACTION_ON_SELECT] = false;
        }
        public function getOption(_arg1:String):Boolean{
            var _local2:Boolean;
            if (this.m_options[_arg1]){
                _local2 = true;
            };
            return (_local2);
        }
        public function setOption(_arg1:String, _arg2:Boolean):void{
            if (this.m_options[_arg1] !== null){
                this.m_options[_arg1] = _arg2;
            };
        }
        public function create(_arg1:String, _arg2:String, _arg3:String=null):void{
            if (this.m_stageToPick[_arg1]){
                this.destroy(_arg1);
            };
            if (!this.m_pickEventCount){
                this.addEventListener(LoaderEvent.LOADED, this.setPicksFolded, false, 0, true);
            };
            this.m_pickEventCount++;
            var _local4:StagePickEffect = new StagePickEffect(m_mapResource);
            _local4.alpha = 0;
            _local4.mouseEnabled = true;
            _local4.mouseChildren = false;
            _local4.addEventListener(MouseEvent.MOUSE_OVER, this.highlightPick, false, 0, true);
            _local4.addEventListener(MouseEvent.MOUSE_UP, this.updateActivePick, false, 0, true);
            this.addChild(_local4);
            _local4.addEventListener(LoaderEvent.LOADED, this.attachPickLoaded, false, 0, true);
            this.m_stageToPick[_arg1] = _local4;
            this.m_picks.push(_local4);
            _local4.setPickType(_arg2, _arg3);
        }
        public function destroy(_arg1:String):void{
            var _local2:StagePickEffect;
            var _local3:int;
            if (this.m_stageToPick[_arg1]){
                _local2 = (this.m_stageToPick[_arg1] as StagePickEffect);
                if (_local2){
                    _local2.removeEventListener(MouseEvent.MOUSE_OVER, this.highlightPick);
                    _local2.removeEventListener(MouseEvent.MOUSE_UP, this.updateActivePick);
                    _local2.parent.removeChild(_local2);
                    _local2.cleanUp();
                    _local3 = this.m_picks.indexOf(_local2);
                    if (-1 != _local3){
                        this.m_picks.splice(_local3, 1);
                    };
                    if (0 == _local3){
                        this.m_stage = null;
                    };
                };
                delete this.m_stageToPick[_arg1];
            };
        }
        public function exists(_arg1:String):Boolean{
            var _local2:Boolean;
            if ((this.m_stageToPick[_arg1] is StagePickEffect)){
                _local2 = true;
            };
            return (_local2);
        }
        public function isViewable(_arg1:String):Boolean{
            var _local2:Boolean;
            if ((this.m_stageToPick[_arg1] is StagePickEffect)){
                _local2 = (this.m_stageToPick[_arg1] as StagePickEffect).visible;
            };
            return (_local2);
        }
        public function viewable(_arg1:String, _arg2:Boolean):void{
            var _local3:StagePickEffect;
            var _local4:Boolean;
            var _local5:int;
            var _local6:int;
            var _local7:int;
            var _local8:StagePickEffect;
            if ((this.m_stageToPick[_arg1] is StagePickEffect)){
                _local3 = (this.m_stageToPick[_arg1] as StagePickEffect);
                _local4 = _local3.visible;
                if (_local4 != _arg2){
                    _local5 = this.m_picks.indexOf(_local3);
                    if (-1 != _local5){
                        this.m_picks.splice(_local5, 1);
                    };
                    if (0 == _local5){
                        this.m_stage = null;
                    };
                    _local3.alpha = 0;
                    _local3.visible = _arg2;
                    if (_arg2){
                        this.m_picks.push(_local3);
                        _local5 = (this.m_picks.length - 1);
                        this.addChildAt(this.m_picks[0], (this.numChildren - 1));
                        _local3.x = 0;
                        _local3.y = 0;
                        _local6 = PICK_SLOT_MAP[(_local5 % PICK_SLOTS)];
                        _local7 = int((_local5 / PICK_SLOTS));
                        if ((((PHASE_UNFOLDING == this.m_phase)) || ((PHASE_UNFOLDED == this.m_phase)))){
                            _local8 = this.m_picks[0];
                            _local3.x = (_local6 * (_local8.width - MOVE_PICK_OFFSET_X));
                            _local3.y = (_local7 * (_local8.height - MOVE_PICK_OFFSET_Y));
                        } else {
                            if ((((((PHASE_FOLDING == this.m_phase)) || ((PHASE_FOLDED == this.m_phase)))) || ((PHASE_NONE == this.m_phase)))){
                                _local3.x = (_local6 * FOLD_PICK_OFFSET_X);
                                _local3.y = (_local7 * FOLD_PICK_OFFSET_Y);
                            };
                        };
                        TweenLite.to(_local3, PICK_TWEEN_IN_TIME, {alpha:1});
                    } else {
                        if (this.m_picks.length){
                            this.addChildAt(this.m_picks[0], (this.numChildren - 1));
                        };
                        if ((PHASE_FOLDED == this.m_phase)){
                            this.setPicksFolded();
                        };
                    };
                };
            };
        }
        public function get active():String{
            var _local1:String;
            if (((!(this.m_stage)) && ((this.m_picks.length > 0)))){
                for (_local1 in this.m_stageToPick) {
                    if (this.m_stageToPick[_local1] === this.m_picks[0]){
                        this.m_stage = _local1;
                    };
                };
            };
            return (this.m_stage);
        }
        public function set active(_arg1:String):void{
            var _local2:StagePickEffect;
            var _local3:int;
            if ((this.m_stageToPick[_arg1] is StagePickEffect)){
                if (((!((_arg1 == this.m_stage))) && ((this.m_picks.length > 0)))){
                    _local2 = (this.m_stageToPick[_arg1] as StagePickEffect);
                    if (((_local2) && (!((_local2 == this.m_picks[0]))))){
                        _local3 = this.m_picks.indexOf(_local2);
                        if (-1 != _local3){
                            this.m_picks[_local3] = this.m_picks[0];
                            this.m_picks[0] = _local2;
                            this.m_stage = _arg1;
                            this.addChildAt(_local2, (this.numChildren - 1));
                            if ((PHASE_FOLDED == this.m_phase)){
                                this.setPicksFolded();
                            };
                            this.dispatchEvent(new Event(MULTIPICK_PICK_CHANGED));
                        };
                    };
                };
            };
        }
        public function get length():int{
            return (this.m_picks.length);
        }
        public function get phase():int{
            return (this.m_phase);
        }
        public function fold():void{
            this.setPicksFolded();
        }
        override public function cleanUp():void{
            var _local1:String;
            var _local2:StagePickEffect;
            this.alpha = 0;
            this.removeEventListener(MouseEvent.MOUSE_OVER, this.animatePicksUnfold);
            this.removeEventListener(MouseEvent.MOUSE_OUT, this.animatePicksFold);
            for (_local1 in this.m_stageToPick) {
                _local2 = this.m_stageToPick[_local1];
                _local2.removeEventListener(MouseEvent.MOUSE_OVER, this.highlightPick);
                _local2.removeEventListener(MouseEvent.MOUSE_UP, this.updateActivePick);
                if (_local2.parent){
                    _local2.parent.removeChild(_local2);
                };
                _local2.cleanUp();
            };
            this.m_picks = new Array();
            this.m_stageToPick = new Dictionary();
            if (this.parent){
                this.parent.removeChild(this);
            };
            this.m_animateEventCount = 0;
            this.m_phase = PHASE_NONE;
            this.m_stage = null;
            this.m_highlighted = null;
        }
        protected function isReady():Boolean{
            var _local1:Boolean;
            if (isMapResourceLoaded){
                if (((((!((PHASE_NONE == this.m_phase))) && ((this.m_picks.length > 0)))) && (!(Global.world.isEditMode)))){
                    _local1 = true;
                };
            };
            return (_local1);
        }
        protected function attachPickLoaded(_arg1:LoaderEvent):void{
            var _local2:StagePickEffect = (_arg1.target as StagePickEffect);
            if (_local2){
                _local2.removeEventListener(LoaderEvent.LOADED, this.attachPickLoaded);
                if (((!(_local2.parent)) || (!((_local2.parent == (this as Sprite)))))){
                    this.addChild(_local2);
                };
                this.m_pickEventCount--;
                if (!this.m_pickEventCount){
                    this.dispatchEvent(new LoaderEvent(LoaderEvent.LOADED));
                };
            };
        }
        protected function animatePicksUnfold(_arg1:MouseEvent):void{
            var _local2:int;
            var _local3:int;
            var _local4:Number;
            var _local5:Number;
            var _local6:StagePickEffect;
            var _local7:int;
            var _local8:GameMode;
            var _local9:String;
            var _local10:GameMode;
            var _local11:String;
            if (this.isReady()){
                if (!this.m_options[OPTION_ACTION_ON_SELECT]){
                    _local8 = Global.world.getTopGameMode();
                    _local9 = getQualifiedClassName(_local8);
                    _local10 = new GMMultiPick();
                    _local11 = getQualifiedClassName(_local10);
                    if (_local11 != _local9){
                        Global.world.addGameMode(_local10);
                    };
                };
                this.m_animateEventCount = this.m_picks.length;
                this.m_phase = PHASE_UNFOLDING;
                _local2 = 0;
                _local3 = 0;
                _local4 = 0;
                _local5 = 0;
                _local6 = this.m_picks[0];
                _local7 = 0;
                while (_local7 < this.m_picks.length) {
                    _local2 = PICK_SLOT_MAP[(_local7 % PICK_SLOTS)];
                    _local3 = int((_local7 / PICK_SLOTS));
                    _local4 = (_local2 * (_local6.width - MOVE_PICK_OFFSET_X));
                    _local5 = ((1 * _local3) * (_local6.height - MOVE_PICK_OFFSET_Y));
                    TweenLite.to(this.m_picks[_local7], 0.3, {
                        x:_local4,
                        y:_local5,
                        onComplete:this.animatePicksUnfolded
                    });
                    _local7++;
                };
            };
        }
        protected function animatePicksUnfolded():void{
            if (this.isReady()){
                this.m_animateEventCount--;
                if (!this.m_animateEventCount){
                    this.m_animateEventCount = 0;
                    this.m_phase = PHASE_UNFOLDED;
                };
            };
        }
        protected function animatePicksFold(_arg1:MouseEvent=null):void{
            var _local2:int;
            if (this.isReady()){
                this.m_phase = PHASE_FOLDING;
                this.m_animateEventCount = this.m_picks.length;
                _local2 = 0;
                while (_local2 < this.m_picks.length) {
                    TweenLite.to(this.m_picks[_local2], 0.3, {
                        x:0,
                        y:0,
                        onComplete:this.animatePicksFolded,
                        ease:Quad.easeIn
                    });
                    _local2++;
                };
            };
        }
        protected function animatePicksFolded():void{
            var _local1:int;
            var _local2:int;
            var _local3:Number;
            var _local4:Number;
            var _local5:int;
            if (this.isReady()){
                this.m_animateEventCount--;
                if (!this.m_animateEventCount){
                    _local1 = 0;
                    _local2 = 0;
                    _local3 = 10;
                    _local4 = 10;
                    _local5 = 0;
                    while (_local5 < this.m_picks.length) {
                        _local1 = PICK_SLOT_MAP[(_local5 % PICK_SLOTS)];
                        _local2 = int((_local5 / PICK_SLOTS));
                        TweenLite.to(this.m_picks[_local5], 0.1, {
                            x:(_local1 * _local3),
                            y:(_local2 * _local4)
                        });
                        _local5++;
                    };
                    this.m_animateEventCount = 0;
                    this.m_phase = PHASE_FOLDED;
                    if (this.m_highlighted){
                        this.m_highlighted.hideHighlight();
                        this.m_highlighted = null;
                    };
                    if (!this.m_options[OPTION_ACTION_ON_SELECT]){
                        Global.world.setDefaultGameMode();
                    };
                };
            };
        }
        protected function setPicksFolded(_arg1:LoaderEvent=null):void{
            var _local2:String;
            var _local3:int;
            var _local4:int;
            var _local5:int;
            var _local6:Number;
            var _local7:Number;
            if (_arg1){
                this.removeEventListener(LoaderEvent.LOADED, this.setPicksFolded);
            };
            if (this.m_highlighted){
                this.m_highlighted.hideHighlight();
                this.m_highlighted = null;
            };
            if (this.m_picks.length > 0){
                if (this.parent){
                    this.parent.removeChild(this);
                };
                this.reposition();
                for (_local2 in this.m_stageToPick) {
                    this.m_stageToPick[_local2].x = 0;
                    this.m_stageToPick[_local2].y = 0;
                };
                _local3 = this.m_picks.length;
                _local4 = 0;
                _local5 = 0;
                _local6 = FOLD_PICK_OFFSET_X;
                _local7 = FOLD_PICK_OFFSET_Y;
                while (--_local3 > -1) {
                    _local4 = PICK_SLOT_MAP[(_local3 % PICK_SLOTS)];
                    _local5 = int((_local3 / PICK_SLOTS));
                    this.m_picks[_local3].x = (_local4 * _local6);
                    this.m_picks[_local3].y = (_local5 * _local7);
                    this.addChildAt(this.m_picks[_local3], (this.numChildren - 1));
                };
            };
            this.m_animateEventCount = 0;
            this.m_phase = PHASE_FOLDED;
            this.reattach();
        }
        protected function updateActivePick(_arg1:MouseEvent):void{
            var _local3:StagePickEffect;
            var _local4:int;
            var _local2:Boolean;
            if (this.isReady()){
                _local3 = (_arg1.target as StagePickEffect);
                if (((_local3) && (!((_local3 == this.m_picks[0]))))){
                    _local4 = this.m_picks.indexOf(_local3);
                    if (-1 != _local4){
                        this.m_picks[_local4] = this.m_picks[0];
                        this.m_picks[0] = _local3;
                        this.m_stage = null;
                        this.addChildAt(_local3, (this.numChildren - 1));
                        _local2 = true;
                    };
                };
                this.animatePicksFold();
                if (_local2){
                    this.dispatchEvent(new Event(MULTIPICK_PICK_CHANGED));
                };
            };
        }
        protected function highlightPick(_arg1:MouseEvent):void{
            var _local2:StagePickEffect;
            if (this.isReady()){
                Global.ui.turnOffHighlightedObject();
                _local2 = (_arg1.target as StagePickEffect);
                if (((this.m_highlighted) && (!((this.m_highlighted == _local2))))){
                    this.m_highlighted.hideHighlight();
                };
                this.m_highlighted = _local2;
                this.m_highlighted.showHighlight();
            };
        }
        override public function animate(_arg1:uint):Boolean{
            return (false);
        }
        public function getFloatOffset():int{
            var _local2:Vector3;
            var _local3:IsoRect;
            var _local1:int;
            if (m_mapResource){
                _local2 = m_mapResource.getReference().getSize();
                _local3 = IsoRect.getIsoRectFromSize(_local2);
                return (-((_local3.top.y + PICK_FLOAT_DISTANCE)));
            };
            return (_local1);
        }
        public function reposition():void{
            var _local1:Vector3 = m_mapResource.getReference().getSize();
            var _local2:IsoRect = IsoRect.getIsoRectFromSize(_local1);
            this.x = _local2.bottom.x;
            this.y = (((_local2.top.y + (this.height / 2)) + PICK_OFFSET_Y) - PICK_FLOAT_DISTANCE);
        }
        override public function reattach():void{
            var _local1:Sprite;
            if (isMapResourceLoaded){
                _local1 = (m_mapResource.getDisplayObject() as Sprite);
                if (((!(this.parent)) || (!((this.parent == _local1))))){
                    this.alpha = 0;
                    this.reposition();
                    _local1.addChild(this);
                    _local1.mouseChildren = true;
                };
                if (0 == this.alpha){
                    TweenLite.to(this, PICK_TWEEN_IN_TIME, {alpha:1});
                };
            };
        }

    }
}//package Classes.effects 
